import router from './router'
import store from './store'
import { ElMessage } from 'element-plus'
import NProgress from 'nprogress' // 进度条
import 'nprogress/nprogress.css' // 进度条样式
import { getToken } from '@/utils/auth'
import getPageTitle from '@/utils/get-page-title'
import { createRouter } from 'vue-router'
NProgress.configure({ showSpinner: false })
import Layout from '@/layout'
const whiteList = ['/login', '/auth-redirect']

let resdata = {
  mu:[
    {
        "menuCode": "ME1619080086988",
        "systemCode": 0,
        "parentMenuCode": "0",
        "menuName": "首页管理",
        "menuType": 1,
        "menuItems": 1,
        "menuUrl": "/users",
        components: "Layout",
        "menuIcon": "2222",
        "menuRemark": "首页信息编写",
        "children": [
            {
                "menuCode": "ME1619086016824",
                "systemCode": 0,
                "parentMenuCode": "ME1619080086988",
                "menuName": "系统管理",
                "menuType": 1,
                "menuItems": 1,
                "menuUrl": "/users/system",
                "menuIcon": "2222",
                "sort": "3",
                "menuRemark": "菜单管理信息",
                "children": [
                    {
                        "menuCode": "ME1619690533549",
                        "systemCode": 0,
                        "parentMenuCode": "ME1619086016824",
                        "menuName": "菜单配置",
                        "menuType": 2,
                        "menuItems": 1,
                        "menuUrl": "/system/menu",
                        components: "/users/system/menu",
                    },
                    {
                      "menuCode": "ME1619690720984",
                      "systemCode": 0,
                      "parentMenuCode": "ME1619086016824",
                      "menuName": "岗位列表",
                      "menuType": 2,
                      "menuItems": 1,
                      "menuUrl": "/staffManage/postList",
                      components: "/users/staffManagement/stationList/postList",
                  }
                ]
            }
        ]
    }
]
}
const routerPackag = routers => {
  routers.filter(itemRouter => {
    if (itemRouter.components&&itemRouter.components != "Layout") {
      router.addRoute("BasicLayout", {
        path: `${itemRouter.menuUrl}`,
        name: itemRouter.menuName,
        component: () => import(`@/views${itemRouter.components}`)
      });
    }
    if (itemRouter.children && itemRouter.children.length) {
      routerPackag(itemRouter.children);
    }
    return true;
  });
};
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  const hasToken = getToken()
  console.log(hasToken)
  if (hasToken) {
    if (to.path === '/login') {
      next({ path: '/' })
      NProgress.done()
    } else {
      const userId = store.getters.userId
      if (userId) {
        next()
      } else {
        try {
          let accessRoutes = []
          const { currentEnterpriseInfo } = await store.dispatch('user/getInfo')
          if (currentEnterpriseInfo.menusVO) {
            let menudata = currentEnterpriseInfo.menusVO
            accessRoutes = await store.dispatch('permission/generateRoutes', resdata.mu)
            routerPackag(resdata.mu)
          }
          
          next({ ...to, replace: true })
        } catch (error) {
          await store.dispatch('user/resetToken')
          Message.error(error || 'Has Error')
          next(`/login?redirect=${to.path}`)
          NProgress.done()
        }
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next(`/login?redirect=${to.path}`)
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})
